package logic

import (
	"context"
	"duo/cache"
	"duo/models/mysql"
	"errors"

	"duo/duorpc/duorpc"
	"duo/duorpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PassUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPassUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PassUpdateLogic {
	return &PassUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PassUpdateLogic) PassUpdate(in *duorpc.PassUpdateRequest) (*duorpc.PassUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("title=?", in.Title).Updates(&user)

	s, _ := cache.Rdb.Get(l.ctx, "sendSmSCode"+in.Title).Result()
	if s != in.SendSmsCode {
		return nil, errors.New("短信验证码错误")
	}

	return &duorpc.PassUpdateResponse{
		UserId: string(user.ID),
	}, nil
}
